package cn.com.dhcc.app.pub.core.util.filefilter;

import java.io.File;
import java.io.FilenameFilter;

/**
 * 通配符文件过滤器
 * @日期：2017/5/4 11:17
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
public class DirFilter implements FilenameFilter {
    private String type;
    public DirFilter(String tp){
        this.type=tp;
    }
    public boolean accept(File fl, String path){
        File file=new File(path);
        String filename=file.getName();
        if (DirFilter.match(type,filename))
            return true;
        if (fl.isDirectory())
            return true;
        else
            return false;
    }
    public static boolean match(String pattern, String str) {
        if (pattern == null || str == null)
            return false;
        boolean result = false;
        char c; // 当前要匹配的字符串
        boolean beforeStar = false; // 是否遇到通配符*
        int back_i = 0;// 回溯,当遇到通配符时,匹配不成功则回溯
        int back_j = 0;
        int i, j;
        for (i = 0, j = 0; i < str.length();) {
            if (pattern.length() <= j) {
                if (back_i != 0) {// 有通配符,但是匹配未成功,回溯
                    beforeStar = true;
                    i = back_i;
                    j = back_j;
                    back_i = 0;
                    back_j = 0;
                    continue;
                }
                break;
            }

            if ((c = pattern.charAt(j)) == '*') {
                if (j == pattern.length() - 1) {// 通配符已经在末尾,返回true
                    result = true;
                    break;
                }
                beforeStar = true;
                j++;
                continue;
            }

            if (beforeStar) {
                if (String.valueOf(str.charAt(i)).equalsIgnoreCase(String.valueOf(c)) ) {
                    beforeStar = false;
                    back_i = i + 1;
                    back_j = j;
                    j++;
                }
            } else {
                if (c != '?' && !String.valueOf(str.charAt(i)).equalsIgnoreCase(String.valueOf(c))) {
                    result = false;
                    if (back_i != 0) {// 有通配符,但是匹配未成功,回溯
                        beforeStar = true;
                        i = back_i;
                        j = back_j;
                        back_i = 0;
                        back_j = 0;
                        continue;
                    }
                    break;
                }
                j++;
            }
            i++;
        }

        if (i == str.length() && j == pattern.length())// 全部遍历完毕
            result = true;
        return result;
    }
//	public static void main(String[] args) {
//		File dir = new File("D:\\dir");
//		List<File> list = new ArrayList<File>();
//		DirFilter df = new DirFilter("*.*");
//		File[] names = dir.listFiles(df);
//        method(dir.listFiles(df), list, df,
//                "*.*", false);
//		for (File file : list)
//			System.out.println(file);
//	}
//    public static void method(File[] files, List<File> list,
//                              FilenameFilter filefilter, String type, boolean flag) {
//        if (files != null) {
//            if (flag) {
//                for (File f : files) {
//                    if (f.isDirectory())
//                        method(f.listFiles(filefilter), list, filefilter, type,
//                                flag);
//                    else if (DirFilter.match(type, f.getName())) {
//                        list.add(f);
//                    }
//                }
//            } else {
//                for (File f : files) {
//                    if (DirFilter.match(type, f.getName())) {
//                        list.add(f);
//                    }
//                }
//            }
//        }
//    }
}

